<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <meta property="wb:webmaster" content="3ccb3cf010ccff41" />
    <title>美国亚马逊今日特惠</title>
    <script src="https://apis.google.com/js/client.js?onload=load"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js" djConfig="parseOnLoad: true, isDebug: true"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dijit/dijit.js" ></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/resources/dojo.css"/>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dijit/themes/tundra/tundra.css"/>
    <script>
    dojo.require("dojo.parser");
	dojo.require("dijit.layout.ContentPane");
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.Button");
	dojo.require("dijit.form.FilteringSelect");
	dojo.require("dijit.form.DropDownButton");
	dojo.require("dojo.io.script");
	dojo.require("dijit.TooltipDialog");
	dojo.require("dijit.Dialog");
	dojo.require("dijit.layout.BorderContainer");
	dojo.require("dijit.form.Textarea");
    </script>

</head>
<!--<script></script>-->
<body class="tundra">
    <h1 id="greeting">Welcome to My Amazon Search Engine</h1>
    
    	<div dojoType = "dijit.layout.ContentPane" id = "headerContainer" style="width:100%">
    		<input dojoType="dijit.form.TextBox"  id="keywordField" style="width:55%;height:20px;"></input>
    		<select dojoType="dijit.form.FilteringSelect" id="searchIndex" title="Select a Catagory" style="width:20%;height:20px;">
    			<option value="All">All</option>
	    		<option value="Baby">Baby</option>
	    		<option value="Beauty">Beauty</option>
	    		<option value="Blended">Blended</option>
	    		<option value="Electronics">Electronics</option>
	    		<option value="Grocery">Grocery</option>	    		
	    		<option value="HealthPersonalCare">HealthPersonalCare</option>
	    		<option value="Jewelry">Jewelry</option>
	    		<option value="Kitchen">Kitchen</option>
	    		<option value="Miscellaneous">Miscellaneous</option>
	    		<option value="OfficeProducts">OfficeProducts</option>
	    		<option value="OutdoorLiving">OutdoorLiving</option>
	    		<option value="SportingGoods">SportingGoods</option>
	    		<option value="Toys">Toys</option>
    		 	<option value="Tools">Tools</option>
				<option value="Watches" >Watches</option>
				<option value="Wireless" >Wireless</option>
				<option value="WirelessAccessories" >WirelessAccessories</option>
    		</select>
    		<select dojoType="dijit.form.FilteringSelect" id="pageIndex" title="Page Index" style="width:10%;height:20px;">
    		 	<option value="1" selected>1</option>
			    <option value="2">2</option>
			    <option value="3">3</option>
			    <option value="4">4</option>
			    <option value="5">5</option>
			    <option value="6">6</option>
			    <option value="7">7</option>
			    <option value="8">8</option>
			    <option value="9">9</option>
			    <option value="10">10</option>
    		</select>
    		<button dojoType="dijit.form.Button"  id="searchButton" style="height:30px;width:20%;vertical-align:baseline;">Start Search！
    		</button>
		<button dojoType="dijit.form.Button"  id="startWeiboButton" style="height:30px;width:20%;vertical-align:baseline;">Share to Weibo
    		</button>
    	</div>
    	<div style="width:1160px; height:1300px">
    	<div id="sidebar" style="width:160px;margin:0;padding:0;float:left;">
    			<div>
		      		<iframe src="http://rcm.amazon.com/e/cm?t=doherty-20&o=1&p=11&l=bn1&mode=hpc&browse=3760901&fc1=000000&lt1=_blank&lc1=3366FF&bg1=FFFFFF&f=ifr" marginwidth="0" marginheight="0" width="160" height="600" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe>
		      	</div>
		      	<div>
		      		<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_521e1c21-590d-4d5a-bf7e-8e15d8d95e5e"  WIDTH="160px" HEIGHT="400px"> <PARAM NAME="movie" VALUE="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fdoherty-20%2F8009%2F521e1c21-590d-4d5a-bf7e-8e15d8d95e5e&Operation=GetDisplayTemplate"><PARAM NAME="quality" VALUE="high"><PARAM NAME="bgcolor" VALUE="#FFFFFF"><PARAM NAME="allowscriptaccess" VALUE="always"><embed src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fdoherty-20%2F8009%2F521e1c21-590d-4d5a-bf7e-8e15d8d95e5e&Operation=GetDisplayTemplate" id="Player_521e1c21-590d-4d5a-bf7e-8e15d8d95e5e" quality="high" bgcolor="#ffffff" name="Player_521e1c21-590d-4d5a-bf7e-8e15d8d95e5e" allowscriptaccess="always"  type="application/x-shockwave-flash" align="middle" height="400px" width="160px"></embed></OBJECT> <NOSCRIPT><A HREF="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fdoherty-20%2F8009%2F521e1c21-590d-4d5a-bf7e-8e15d8d95e5e&Operation=NoScript">Amazon.com Widgets</A></NOSCRIPT>
		      	</div>
    		</div>
    		<div dojoType = "dijit.layout.ContentPane" id = "resultPane" style="width:1000px;float:left;margin:0;padding: 0;"></div>
    	</div>
<!--     	<table style="width: 100%; height:100%;"> -->
<!-- 		  <tbody> -->
<!-- 		    <tr style="height:1300px;"> -->
<!-- 		      <td style="width:1000px"> -->
<!-- 			      <div dojoType = "dijit.layout.ContentPane" id = "resultPane" style="width:1000px;"> -->
<!-- 	    		</div> -->
<!-- 		      </td> -->
<!-- 		      <td  > -->
<!-- 		      	<div> -->
<!-- 		      		<iframe src="http://rcm.amazon.com/e/cm?t=doherty-20&o=1&p=11&l=bn1&mode=hpc&browse=3760901&fc1=000000&lt1=_blank&lc1=3366FF&bg1=FFFFFF&f=ifr" marginwidth="0" marginheight="0" width="160" height="600" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe> -->
<!-- 		      	</div> -->
<!-- 		      	<div style="margin-top:0;"> -->
<!-- 		      		<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_521e1c21-590d-4d5a-bf7e-8e15d8d95e5e"  WIDTH="160px" HEIGHT="400px"> <PARAM NAME="movie" VALUE="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fdoherty-20%2F8009%2F521e1c21-590d-4d5a-bf7e-8e15d8d95e5e&Operation=GetDisplayTemplate"><PARAM NAME="quality" VALUE="high"><PARAM NAME="bgcolor" VALUE="#FFFFFF"><PARAM NAME="allowscriptaccess" VALUE="always"><embed src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fdoherty-20%2F8009%2F521e1c21-590d-4d5a-bf7e-8e15d8d95e5e&Operation=GetDisplayTemplate" id="Player_521e1c21-590d-4d5a-bf7e-8e15d8d95e5e" quality="high" bgcolor="#ffffff" name="Player_521e1c21-590d-4d5a-bf7e-8e15d8d95e5e" allowscriptaccess="always"  type="application/x-shockwave-flash" align="middle" height="400px" width="160px"></embed></OBJECT> <NOSCRIPT><A HREF="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fdoherty-20%2F8009%2F521e1c21-590d-4d5a-bf7e-8e15d8d95e5e&Operation=NoScript">Amazon.com Widgets</A></NOSCRIPT> -->
<!-- 		      	</div> -->
		      	
<!-- 		      </td> -->
<!-- 		    </tr> -->
<!-- 		  </tbody> -->
<!-- 		</table> -->
    	
	<div id="weibodialog" dojoType="dijit.Dialog"  title="Share to weibo" open=false autofocus=false>
	    <div id="weibotextedit" dojoType="dijit.form.Textarea">
	    </div>
	    <div id="saletext" dojoType="dijit.form.Textarea">
	    </div>
	    <div id="saleprice" dojoType="dijit.form.TextBox">
	    </div>
	    <div id="asin" dojoType="dijit.form.TextBox" style="display:none;">
	    </div>
	    <div id="imgurl" dojoType="dijit.form.TextBox" style="display:none;">
	    </div>
	    <div id="shorturl" dojoType="dijit.form.TextBox">
	    </div>
	    <div dojoType="dijit.form.Button" id="weibotasksubmit">Add to weibo queue
	    </div>
	</div>
	<div id="twitterdialog" dojoType="dijit.Dialog"  title="Share to twitter" open=false autofocus=false>
	    <div id="tdialogmessagearea"></div>
	    <div id="twittertextedit" dojoType="dijit.form.Textarea">
	    </div>
	    <div id="tsaletext" dojoType="dijit.form.Textarea">
	    </div>
	    <div id="tsaleprice" dojoType="dijit.form.TextBox">
	    </div>
	    <div id="tasin" dojoType="dijit.form.TextBox" style="display:none;">
	    </div>
	    <div id="timgurl" dojoType="dijit.form.TextBox" style="display:none;">
	    </div>
	    <div id="dealrate" dojoType="dijit.form.TextBox" style="display:none;">
	    </div>
	    <div id="tshorturl" dojoType="dijit.form.TextBox">
	    </div>
	    <div>
		<div dojoType="dijit.form.Button" id="twittertasksubmit">Confirm
		</div>
	    </div>
	</div>
    	
</body>
<script type="text/javascript">

dojo.addOnLoad(function(){
    
	dijit.byId("twittertasksubmit").onClick = function(){
	    dojo.xhrGet({
		url: "/enqueproductqueue",
		content: {
			twitterproductdesc : dijit.byId("twittertextedit").get("Value"),
			saletext:	dijit.byId("tsaletext").get("Value"),
			saleprice:	dijit.byId("tsaleprice").get("Value"),
			shorturl:	dijit.byId("tshorturl").get("Value"),
			dealrate:	dijit.byId("dealrate").get("Value"),
			asin:		dijit.byId("tasin").get("Value"),
			imgurl:		dijit.byId("timgurl").get("Value")},
		handleAs: "json",
		sync : false,
		load: function(response, ioArgs){
			dojo.byId("tdialogmessagearea").innerHTML = "Posted it to twitter";
			dijit.byId("twittertextedit").set("Value","");
			dijit.byId("tsaletext").set("Value","");
			dijit.byId("tsaleprice").set("Value","");
			dijit.byId("tshorturl").set("Value","");
			dijit.byId("dealrate").set("Value","");
			dijit.byId("tasin").set("Value","");
			dijit.byId("timgurl").set("Value","");
			dijit.byId("twitterdialog").hide();
			dojo.byId("tdialogmessagearea").innerHTML = "";
			
		}
	    });
	}
	

	
	dijit.byId("startWeiboButton").onClick = function(){
	    window.open("https://api.weibo.com/oauth2/authorize?client_id=3201307572&response_type=code&redirect_uri=" + encodeURIComponent("http://aaasearchengine.appspot.com/weibopostprocessor"));
	}
	
	dijit.byId("weibotasksubmit").onClick = function(){
	    if (dijit.byId("weibotextedit").get("Value").length > 100){
		alert("too long!");
	    }else{
		dojo.xhrGet({
		    url : "/enqueueweibotask",
		    content : {weiboproductdesc : dijit.byId("weibotextedit").get("Value"),
				saletext:	dijit.byId("saletext").get("Value"),
				saleprice:	dijit.byId("saleprice").get("Value"),
				shorturl:	dijit.byId("shorturl").get("Value"),
				asin:		dijit.byId("asin").get("Value"),
				imgurl:		dijit.byId("imgurl").get("Value")},
		    sync : false,
		    load: function(response, ioArgs){
			    
			    dijit.byId("weibotextedit").set("Value","");
			    dijit.byId("saletext").set("Value","");
			    dijit.byId("saleprice").set("Value","");
			    dijit.byId("shorturl").set("Value","");
			    dijit.byId("asin").set("Value","");
			    dijit.byId("imgurl").set("Value","");
			    dijit.byId("weibodialog").hide();
		    }
		});
	    }
	    
	}
			
	
	dijit.byId("searchButton").onClick = function(){
		var xhrArgs = {
			url: "/amazondealssearchengine",
			content: {"keyword" : dijit.byId("keywordField").get("Value"), "searchindex" : dijit.byId("searchIndex").get("Value"), "pageindex" : dijit.byId("pageIndex").get("Value")},
			handleAs: "json",
			sync : false,
			load: function(response, ioArgs){
				showPageResult(response);
				return response;
			}

		};		
		dojo.xhrGet(xhrArgs);	
	}
	
	function showPageResult(resultJson){
		var availablePT = new Array();
		dijit.byId("resultPane").destroyDescendants(false);
		dojo.forEach(resultJson, function(x, idx){
			//var newdiv = dojo.create("div", {"id" : "bcContainerDiv" + x.asin, "style" : "width:100%; height:200px;"}, "resultPane");
			//var resultEntryPane = new dijit.layout.ContentPane({
			//	id: 	"entryPane" + x.asin,
			//	style: "height: 200px; margin-top:10px; width:100%"
			//    }
			//);
			//resultEntryPane.placeAt("resultPane");
			//resultEntryPane.startup();
			
			//var newdiv = dojo.create("div", {"id" : "bcContainerDiv" + x.asin, "style" : "width:100%; height:200px;"}, "resultPane");
			var bc = new dijit.layout.BorderContainer(
			    {
				id: "bc" + x.asin,
				design : "sidebar",
				style: "height:120px; width:1000px;border:3px;margin-top:10px;"
			    }
			);
			bc.placeAt("resultPane");
			bc.startup();
			
			
			var leftPane = new dijit.layout.ContentPane({
				id : "leftpane" + x.asin,
				region: "left",
				style: "width:100px;height:100%;splitter:true;",
				splitter: true
			    },
			    document.createElement("div")
			);
			
			var centralPane = new dijit.layout.ContentPane({
				id : "centerpane" + x.asin,
				region: "center",
				style: "width:650px;splitter:true;height:100%;",
				splitter: true
			    },
			    document.createElement("div")
			);

			var buttonPane = new dijit.layout.ContentPane({
				id : "buttonpane" + x.asin,
				region: "right",
				style: "width:150px;splitter:true;height:100%;",
				splitter: true
			    },
			    document.createElement("div")
			);
			leftPane.startup();
			centralPane.startup();
			buttonPane.startup();
			
			bc.addChild(leftPane);
			bc.addChild(centralPane);
			bc.addChild(buttonPane);
			
			dojo.create("img",{"src" : x.imageurl, "height": 75, "width":74 }, "leftpane" + x.asin);
			var title = new dijit.form.TextBox({
				value: x.title,
				style: "width:630px;"
			});
			title.placeAt("centerpane" + x.asin);
			
			dojo.create("br",{},"centerpane" + x.asin);
			var price = new dijit.form.TextBox({
				id: "priceText" + x.asin,
				value: x.price,
				style: "width:100px;"
			});
			price.placeAt("centerpane" + x.asin);
			var dealinfo = "Not a Deal";
			switch(x.dealrate){
			    case 1:
			      dealinfo = "A third party low price";
			      break;
			    case 2:
			      dealinfo = "An amazon low price";
			      break;
			    case 4:
			      dealinfo = "A third party Deal!";
			      dijit.byId("centerpane" + x.asin).set("style", "background-color: #FFDDAA");
			      break;
			    case 5:
			      dealinfo = "An amazon Deal!";
			      dijit.byId("centerpane" + x.asin).set("style", "background-color: #FF8888");
			      break;
			    default:
			      dealinfo = "No deals available";
			      dijit.byId("centerpane" + x.asin).set("style", "background-color: #DDDDDD");
			}
			
			var dealText = new dijit.form.TextBox({
				value: dealinfo,
				style: "width:200px;"
			});
			dealText.placeAt("centerpane" + x.asin);
			
			var asinbox = new dijit.form.TextBox({
				value: x.asin,
				style: "width:150px;"
			});
			asinbox.placeAt("centerpane" + x.asin);
			
			var link = new dijit.form.Button({
				label: "Amazon",
				style: "vertical-align:baseline;",
				onClick: function(){window.open(x.productlink);}
				
			});
			link.placeAt("buttonpane" + x.asin);
			
			var trackbutton = new dijit.form.Button({
			    label: "Track it!",
			    style: "vertical-align:baseline;",
			    onClick: function(){
			    	dojo.xhrGet({
			    		url: "/enqueproductqueue",
			    		content: {
			    			twitterproductdesc : x.title,
			    			saleprice:	x.price,
			    			shorturl:	x.productlink,
			    			dealrate:	x.dealrate,
			    			asin:		x.asin,
			    			imgurl:		x.imageurl,
			    			operation:	"track"},
			    		handleAs: "json",
			    		sync : false,
			    		load: function(response, ioArgs){
			    			alert("This product has been added to track list");
			    		}
			    	  });
			    }
			});
			trackbutton.placeAt("buttonpane" + x.asin);
			
			var tweetbutton = new dijit.form.Button({
			    label: "Twitter",
			    style: "vertical-align:baseline;",
			    onClick: function(){
				var request = gapi.client.urlshortener.url.insert({
				    'resource': {'longUrl': x.productlink} 
				});
				request.execute(function(response) {
				    //window.open("https://twitter.com/intent/tweet?" + encodeURI("hashtags=AmazonDeals&original_referer=aaasearchengine&source=tweetbutton&text=" + x.title + ",now its only " + x.price + " " + response.id));
				    dijit.byId("twitterdialog").show();
				    dijit.byId("twittertextedit").set("Value", x.title );
				    dijit.byId("tshorturl").set("Value", response.id);
				    dijit.byId("tsaleprice").set("Value", x.price);
				    dijit.byId("dealrate").set("Value", x.dealrate);
				    dijit.byId("tsaletext").set("Value", "Sale! ");
				    dijit.byId("timgurl").set("Value", x.imageurl);
				    dijit.byId("tasin").set("Value", x.asin);
				});
			    }
			});
			tweetbutton.placeAt("buttonpane" + x.asin);
			
			var weibobutton = new dijit.form.Button({
			    label: "Weibo",
			    style: "vertical-align:baseline;",
			    onClick: function(){
				var request = gapi.client.urlshortener.url.insert({
				    'resource': {'longUrl': x.productlink} 
				});
				request.execute(function(response) {
				    dijit.byId("weibodialog").show();
				    dijit.byId("weibotextedit").set("Value", x.title );
				    dijit.byId("shorturl").set("Value", response.id);
				    dijit.byId("saleprice").set("Value", x.price);
				    dijit.byId("saletext").set("Value", "降价!");
				    dijit.byId("imgurl").set("Value", x.imageurl);
				    dijit.byId("asin").set("Value", x.asin);
				});
			    }
			    
				
			});
			weibobutton.placeAt("buttonpane" + x.asin);
			bc.layout();
			dijit.byId("resultPane").resize()
			
			
		});
		
	}
	
	gapi.client.setApiKey('AIzaSyCgxE969BDbllbqyyFiG2VQBDvubbO19Ik');
	gapi.client.load('urlshortener', 'v1');
	
});
</script>
</html>